package cn.licoy.wdog.core.controller.hospital;

import cn.licoy.wdog.common.annotation.SysLogs;
import cn.licoy.wdog.common.bean.ResponseCode;
import cn.licoy.wdog.common.bean.ResponseResult;
import cn.licoy.wdog.core.config.jwt.JwtToken;
import cn.licoy.wdog.core.dto.SignInDTO;
import cn.licoy.wdog.core.dto.hospitalUser.*;
import cn.licoy.wdog.core.entity.hospital.HospitalUser;
import cn.licoy.wdog.core.service.hospital.HospitalUserService;
import cn.licoy.wdog.core.service.hospital.SmscodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Sunne
 * Create time 2018/6/21 15:45
 * @return null
 **/
@RestController
@RequestMapping(value = "/hospitaluser")
@Api(tags = {"第一期--医院用户操作"})
public class HospitalUserController {

    @Autowired
    private HospitalUserService hospitalUserService;

    @Autowired
    private SmscodeService smscodeService;

    @PostMapping(value = {"/sign-in"})
    @ApiOperation(value = "登录")
    @SysLogs("登录")
    public ResponseResult<ReturnDigninDto> signIn(@RequestBody @Validated @ApiParam(value = "登录数据",required = true) SignInDTO signInDTO){
        HospitalUser user = hospitalUserService.signIn(signInDTO);
        ReturnDigninDto dto = new ReturnDigninDto();
        dto.setToken(((JwtToken)SecurityUtils.getSubject().getPrincipal()).getToken());
        dto.setUser(user);
        return ResponseResult.e(ResponseCode.SIGN_IN_OK,dto);
    }
    @PostMapping(value = {"/regist"})
    @ApiOperation(value = "医院用户注册")
    @SysLogs("医院用户注册")
    public ResponseResult<ReturnDigninDto> add(@RequestBody @Validated @ApiParam(value = "用户数据") HospitalUserDto hospitalUserDto){
        HospitalUser user = hospitalUserService.add(hospitalUserDto);
        ReturnDigninDto dto = new ReturnDigninDto();
        dto.setToken(((JwtToken)SecurityUtils.getSubject().getPrincipal()).getToken());
        dto.setUser(user);
        return ResponseResult.e(ResponseCode.OK,dto);
    }

    @PostMapping(value = {"/regcheck"})
    @ApiOperation(value = "新增医院用户发送验证码")
    @SysLogs("新增医院用户发送验证码")
    public ResponseResult<String> regcheck(
            @ApiParam(value = "手机号")@RequestParam(value = "phone", required = false) String phone,
            @ApiParam("验证码类型") @RequestParam(value = "codeType", required = false) String codeType){
       String rcode = smscodeService.sencode(phone,codeType);
       if("-1".equals(rcode)||"-"==rcode){
           return ResponseResult.e(ResponseCode.FAIL);
       }
        SendMsg sm = new SendMsg();
       sm.setMsg(rcode);
        return ResponseResult.e(ResponseCode.OK,"",rcode);
    }

    @PostMapping(value = {"/verificationCode"})
    @ApiOperation(value = "验证验证码")
    @SysLogs("验证验证码")
    public ResponseResult verificationCode(
            @ApiParam(value = "手机号")@RequestParam(value = "phone", required = false) String phone,
            @ApiParam("验证码") @RequestParam(value = "code", required = false) String code,
            @ApiParam("验证码类型") @RequestParam(value = "codetype", required = false) String codetype
    ){
       String state = smscodeService.verificationCode(phone,code,codetype);
        return ResponseResult.e(ResponseCode.OK,state);
    }
    @PostMapping(value = {"/updateHospitalUserInfo"})
    @ApiOperation(value = "修改医生用户信息")
    @SysLogs("修改医生用户信息")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public ResponseResult updateHospitalUserInfo(
            @RequestBody @Validated @ApiParam(value = "用户数据") UpdateHouitalUserDto dto){
        hospitalUserService.updateUserinfo(dto);
        return ResponseResult.e(ResponseCode.OK,"操作成功");
    }

    @PostMapping(value = {"/updatepassword"})
    @ApiOperation(value = "修改密码")
    @SysLogs("修改密码")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public ResponseResult updatepassword(@RequestBody @ApiParam(value = "修改密码实体") UpdatePasswordDto updatePasswordDto
    ){
        hospitalUserService.updatePassword(updatePasswordDto);
        return ResponseResult.e(ResponseCode.OK,"密码修改成功");
    }

}